package com.julu.o2o.util;

import java.util.ArrayList;
import java.util.List;

public class ListSplitUtil {
	/**
     * 分割List
     * @author bianrx
     * @date 2012.1.13
     * @param list 待分割的list
     * @param pageSize 每段list的大小
     * @return List<<List<T>> 
     */
     public static <T> List<List<T>> split(List<T> list, int pageSize) {    	
        int listSize = list.size();                                                           
        int page = (listSize + (pageSize-1))/ pageSize;         
        List<List<T>> listArray = new ArrayList<List<T>>();
        for(int i=0;i<page;i++) {                   
            List<T> subList = new ArrayList<T>();   
            for(int j=0;j<listSize;j++) {                     
                int pageIndex = ( (j + 1) + (pageSize-1) ) / pageSize; 
                if(pageIndex == (i + 1)) {                             
                    subList.add(list.get(j));                                              
                }                
                if( (j + 1) == ((j + 1) * pageSize) ) {                             
                    break;
                }
            }
            listArray.add(subList);                                                        
        }
        return listArray;
    }
    
    public static void main(String[] s) {        
        List<String> strlist = new ArrayList<String>();
        for(int i=0;i<120;i++) {
            strlist.add("aa" + (i+1));
        }
        List<List<String>> list = split(strlist, 17);        
        int index = 1;
        for(List<String> strlist2: list) {
            System.out.println(index++);
            System.out.println("----------------------------------");
            for(String str: strlist2) {
                System.out.print(str + "\t");
            }
            System.out.println("END");
            System.out.println();
        }
    }
    
}
